<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Account
 *
 * @author bom
 */
class Account extends Zend_Db_Table_Abstract {

    //put your code here
    protected $_name = 'user_account';
    protected $_primary = 'user_id';

    //protected $_dependentTables = array('country');

    /* protected $_referenceMap    = array(
      'city_country' => array(
      'columns'           => 'country_id',
      'refTableClass'     => 'Country',
      'refColumns'        => 'id'
      )
      ); */
    public function isUsernameUsed($username) {
        $select = $this->select()->where("username=?", $username);
        $rowSet = $this->fetchRow($select);
        if (isset($rowSet->user_id)) {
            return true;
        } else {
            return false;
        }
    }

    public function isEmailUsed($email) {
        $select = $this->select()->where("email=?", $email);
        $rowSet = $this->fetchRow($select);
        if (isset($rowSet->user_id)) {
            return true;
        } else {
            return false;
        }
    }

    public function getUserByUsername($username) {
        $userRowset = $this->fetchAll($this->select()->where('username = ?', $username));
        $user = $userRowset->current();
        return $user;
    }

    public function getUserById($id){
        $userRowset = $this->fetchAll($this->select()->where('user_id = ?', $id));
        $user = $userRowset->current();
        return $user;
    }

    public function getAllUsers($page = 0, $order = "u.user_id desc"){
    	$tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $tb->select()
        			->from(array('u' => 'user_account'))
       				->join(array('g' => 'user_group'), 'u.group_id = g.group_id')
        			->order($order);
        $paginator = Zend_Paginator::factory($select);
    	$paginator->setItemCountPerPage(Zend_Registry::get('page_size'));
    	$paginator->setCurrentPageNumber($page); 
    	
        return $paginator;
    }
}

?>
